package org.maxgroundstation.groundstation.view;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.plaf.metal.MetalLookAndFeel;
import javax.swing.plaf.metal.OceanTheme;

import org.maxgroundstation.groundstation.GroundStation;

/**
 * A frame for displaying ground station information and controls.
 * @author Scott Davis
 * 
 * Copyright (C) 2010  Scott Davis
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
@SuppressWarnings("serial")
public class GroundStationFrame extends JFrame {

	// Data members.
	private GroundStation gs;
	
	/**
	 * Constructor
	 * @param gs the ground station.
	 */
	public GroundStationFrame(GroundStation gs) {
		super("MAX Ground Station");
		
		// Initialize data members.
		this.gs = gs;
		
		// Set the look and feel.
		setLookAndFeel();
		
		// Set frame size
		Dimension frame_size = null;
		Dimension screen_size = Toolkit.getDefaultToolkit().getScreenSize();
		// Make frame size 80% of screen size.
		frame_size = new Dimension(screen_size);
		if (screen_size.width > 800) {
			frame_size = new Dimension((int) Math.round(screen_size
					.getWidth() * .9D), (int) Math.round(screen_size
					.getHeight() * .9D));
		}
		setSize(frame_size);

		// Center frame on screen
		setLocation(((screen_size.width - frame_size.width) / 2),
				((screen_size.height - frame_size.height) / 2));
		
		// Create the control pane.
		JPanel controlPane = new ControlPanel(gs);
		
		// Create scroll pane for the control pane.
        JScrollPane controlScrollPane = new JScrollPane(controlPane, 
                JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		
		// Create the view pane.
		JTabbedPane viewPane = new ViewPanel(gs);
		
		// Create a split pane with the control pane on the left and the view pane on the right.
		JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, controlScrollPane, viewPane);
		splitPane.setOneTouchExpandable(true);
		splitPane.setDividerLocation(325);
		add(splitPane);
		
		// Set the panel visible.
		setVisible(true);
		
		// Add window listener to exit application.
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                shutdown();
                System.exit(0);
            }
        });
	}
	
	/**
	 * Set the look & feel of the GUI.
	 */
	private void setLookAndFeel() {
		try {
			boolean foundNimbus = false;
			for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					UIManager.setLookAndFeel(info.getClassName());
					foundNimbus = true;
					break;
				}
			}
			
			if (!foundNimbus) {
				MetalLookAndFeel.setCurrentTheme(new OceanTheme());
				UIManager.setLookAndFeel(new MetalLookAndFeel());
			}
			
			SwingUtilities.updateComponentTreeUI(this);
		} catch (Exception e) {
			e.printStackTrace(System.err);
		}
	}
    
    /**
     * Shuts down the ground station.
     */
    private void shutdown() {
        gs.shutdown();
    }
}